<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */


require_once(ROOT_PATH."includes/phpsvnclient.php");

$path = str_replace('$LANG',$team->lang,$prj->path);
$path .= str_replace('$LANG',$team->lang,$subprj->path);

$svn  = new phpsvnclient;
$svn->setRespository(SVN_ROOT);

$svnlist = $svn->getDirectoryFiles($path);
/* FOR TESTING	ONLY
	$ar = array("path"=>"trunk/aunarchivo.es.po");
	$ar1 = array("path"=>"trunk/otroarchivo.es.po");
	$ar2 = array("path"=>"trunk/masarchivo.es.ma");
	
	$svnlist = array($ar,$ar1,$ar2);
	*/

if (count($svnlist)>0) {
	if ($update!="true") {
		$f=new files($prj->id);
		$f->teamid=$team->id;
		$f->subprj=$subprj->id;
		
		$h=new history($prj->id);
		$h->teamid=$f->teamid;
		$h->subprj=$f->subprj;
		$loader->execute($h->cleanTeam());
		$ah=$db->affected_rows();
		$loader->execute($f->deleteTeam());
		$af=$db->affected_rows();
		if ($af>0) {
			echo ('Deleted '.$ah.' existing history rows.<br>');
			echo ('Deleted '.$af.' existing file rows.<br>');	
		}			
	}
}

$saved=0;
$data = array();
foreach ($svnlist as $key => $obj) {
	$f = $obj['path'];
	$fname = substr($f,strrpos($f,'/')+1);
	$fext = substr($fname,strrpos($fname,".")+1);
	if ($fext=="po") {
		array_push($data,$fname);
		$file = new files($prj->id);
		$file->teamid=$team->id;
		$file->name=$fname;
		$file->path=$subprj->name;
		$file->subprj=$subprj->id;
		
		if ($update=="true") {
			if ($loader->getBy($file,$file->findFile())===false) {
				echo "Added ".$file->name;
				if ($console) {
					echo "\n";
				} else {
					echo "<br>";
				}
				$loader->save($file);
				$saved +=1;	
			}
		} else {
			$loader->save($file);
			$saved +=1;
		}
	} else {
		echo "Invalid file ".$fname;
		if ($console) {
			echo "\n";
		} else {
			echo "<br>";
		}
	}
	
}

$del=0;

if ($update=="true") {
	$file = new files($prj->id);
	$file->teamid=$team->id;
	$file->path=$subprj->name;
	$file->subprj=$subprj->id;
	$list = $loader->loadListBy($file,$file->loadfiltered());

	foreach ($list as $obj) {
		if (in_array($obj->name,$data)===false) {
			/*
			$h = new history($prj->id);
			$h->fileid=$obj->id;
			$loader->delete($h);
			$loader->delete($obj);
			*/
			$del+=1;
			
			if ($console) {
				echo "Remove ".$obj->name."\n";
			} else {
				$tm->assign("name","Remove");
				$tm->assign("action","removeFile('$obj->id','$prj->id')");
				$button = $tm->fetch("button.tpl");
				echo "<span id=\"filerow$obj->id\">Remove ".$obj->name." $button</span><br>";	
			}
		}
	}
	
}

if ($console) {
	echo ("Processed ".$team->name."\n".$saved." files created.\n");
} else {
	echo ("Processed ".$team->name."<br>".$saved." files created.<br>");	
}


if ($del>0) {
	echo ($del." files should be removed.");
	if ($console) {
		echo "\n";
	} else {
		echo "<br>";
	}
}

?>
